/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import static gui.JPanelSearch.jLabelSearchLoading;

import java.awt.Component;
import java.awt.FlowLayout;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import core.exceptions.DBAccessException;
import core.objects.Book;
import core.objects.User;
import db.DBAccessEngine;
import db.threads.DBAccessThread;
import db.threads.IDBAccessThread;

class JPanelUsersSearch extends JPanelSearch {

    private JRadioButton jradioUserDetails;
    private JRadioButton jradioBooks;

    public JPanelUsersSearch() {
        jradioUserDetails.setSelected(true);
    }

    @Override
    void search() {
        super.search();
        DBAccessThread.invoke(new IDBAccessThread() {
            @Override
            public Object onCreateQuery() throws DBAccessException {
                Collection<User> users = usersSearch(jTextFieldSearch.getText());
                return users;
            }

            @SuppressWarnings("unchecked")
			@Override
            public void onAccessFinished(Object response) {
                jPanelContents.removeAll();
                Collection<User> users = (Collection<User>) response;
                for (User user : users) {
                    jPanelContents.add(new JPanelUserInfo(user));
                }
                jLabelSearchLoading.setIcon(null);
                jPanelContents.setVisible(false);
                jPanelContents.setVisible(true);
                if (jPanelContents.getComponents().length == 0) {
                    jPanelContents.add(GUIUtils.getNotFoundPanel());
                }
            }

            @Override
            public void onAccessFailed(DBAccessException e) {
                jLabelSearchLoading.setIcon(null);
                GUIUtils.showMessageError(e, jPanelContents);
            }
        });
        jPanelContents.setVisible(false);
        jPanelContents.setVisible(true);

    }

    void search(User user) {
        super.search();
        jPanelContents.add(new JPanelUserInfo(user));
        jLabelSearchLoading.setIcon(null);
        jPanelContents.setVisible(false);
        jPanelContents.setVisible(true);
    }

    void search(final Book book) {
        super.search();
        final JPanelUsersSearch thisObj = this;
        DBAccessThread.invoke(new IDBAccessThread() {

            @Override
            public Object onCreateQuery() throws DBAccessException {
                return DBAccessEngine.User().select(book.getID(), new Book(0, ""));
            }

            @SuppressWarnings("unchecked")
			@Override
            public void onAccessFinished(Object response) {
                for (User user : (List<User>) response) {
                    jPanelContents.add(new JPanelUserInfo(user));
                }
                jLabelSearchLoading.setIcon(null);
                jPanelContents.setVisible(false);
                jPanelContents.setVisible(true);
                if (jPanelContents.getComponents().length == 0){
                    jPanelContents.add(GUIUtils.getNotFoundPanel());
                }
            }

            @Override
            public void onAccessFailed(DBAccessException e) {
                GUIUtils.showMessageError(e, thisObj);
            }
        });
    }

    @SuppressWarnings("unchecked")
	public Collection<User> usersSearch(String searchStr) {
        List<User> users = null;
        Set<User> allUsers = new HashSet<>();
        try {
            if (jradioUserDetails.isSelected()) {
                users = (List<User>) DBAccessEngine.User().selectContains(searchStr, true);
                allUsers.addAll(users);
                users = (List<User>) DBAccessEngine.User().selectContains(searchStr, new User(0, ""), true);
                allUsers.addAll(users);
            } else {
                users = (List<User>) DBAccessEngine.User().selectContains(searchStr, new Book(0, ""), true);
                allUsers.addAll(users);
            }
        } catch (DBAccessException ex) {
            GUIUtils.showMessageError(ex, new Component() {
            });
        }
        return allUsers;
    }

    @Override
    void setComponentsInitialTextValues() {
        initialSearchBoxValue = "Lets find a Friend!";
        initialSearchFormHeaderText = "Find-a-Friend";
    }

    @Override
    protected void initAuxComponents() {
        super.initAuxComponents();
        JPanel jpanelSearchBoxSecondLine = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
        jpanelSearchBoxSecondLine.setOpaque(false);

        JLabel jlabelSearchIn = new JLabel("Search in:");
        GUIUtils.setJComponentFontAndColor(jlabelSearchIn, 13);
        jpanelSearchBoxSecondLine.add(jlabelSearchIn);

        ButtonGroup bg = new ButtonGroup();
        jradioUserDetails = new JRadioButton("Users details");
        GUIUtils.setJComponentFontAndColor(jradioUserDetails, 13);
        bg.add(jradioUserDetails);
        jpanelSearchBoxSecondLine.add(jradioUserDetails);
        jradioBooks = new JRadioButton("Users' books");
        GUIUtils.setJComponentFontAndColor(jradioBooks, 13);
        bg.add(jradioBooks);
        jpanelSearchBoxSecondLine.add(jradioBooks);

        jPanelSearch.add(jpanelSearchBoxSecondLine);
    }
}
